Digital Galton Board VGA Simulation

A real-time statistical physics simulation built on the Raspberry Pi Pico (RP2040) demonstrating the Central Limit Theorem in action.

Contributors: Tony Kariuki and Jeremy Simon

System Architecture

This system merges real-time graphics, hardware-accelerated audio, and concurrent protothreads to simulate up to 700 bouncing balls simultaneously at a stable 30 frames-per-second.

  • Microcontroller: RP2040 (Raspberry Pi Pico) utilizing fixed-point arithmetic to avoid floating-point overhead.
  • Visuals: Custom VGA rendering driver outputting a real-time histogram and live physics.
  • Audio: Direct Memory Access (DMA) triggered sound effects driving an SPI-connected Digital-to-Analog Converter (DAC) at ~44kHz.
  • Concurrency: Three discrete protothreads handling VGA/Physics, state machine user inputs, and switch debouncing.

Statistics and Mathematics

The Galton Board is a classic demonstration of the Central Limit Theorem. As balls fall, they have a 50% probability of bouncing left or right at each peg. Each row acts as an independent Bernoulli trial.

For a board with 16 rows, the binomial distribution is:

XBinomial(n=16,p=0.5)X \sim \text{Binomial}(n=16, p=0.5)

As the number of rows (nn) increases, the discrete binomial distribution smoothly converges into a continuous Gaussian normal curve (N(μ,σ2)N(\mu, \sigma^2)). The expected mean (μ\mu) and standard deviation (σ\sigma) for our 16-row setup are calculated as follows:

Mean

μ=np=16×0.5=8\mu = np = 16 \times 0.5 = 8

Standard Deviation

σ=np(1p)=4=2\sigma = \sqrt{np(1-p)} = \sqrt{4} = 2
VGA Output of the Galton Board

Goal for final simulation. Provided by Hunter Adams and Bruce Land

animation.c
// Fixed-Point Physics Structs
typedef struct {
    fix15 x;
    fix15 y;
    fix15 vx;
    fix15 vy;
    int last_peg;
} Ball;

// State Machine Debouncing
case MAYBE_PRESSED:
    if (keypress == possible_press && keypress == 0) {
        switch (button_state) {
            case RESET:
                reset_histogram();
                button_state = ADJUST_BALLS;
                break;
            // State transitions...
        }
        debounce_state = PRESSED;
    }

User Control

A hardware potentiometer mapped to the ADC allows real-time tuning of simulation parameters.

Adjust Active Ball Count
Modify Coefficient of Restitution